package com.dai.chun.dao.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;

/**
 * 过滤器：拦截没有登陆的访问
 * @author Administrator
 *
 */
public class IsLoginFilter implements Filter{
	Logger logger = Logger.getLogger(IsLoginFilter.class);
	public void destroy() {
		
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		HttpSession session = req.getSession(false);
		try {
			if(session!= null){//session不为空
					//员工或者管理员登陆
					if(session.getAttribute("admin")!=null||session.getAttribute("employee")!=null){
						chain.doFilter(request, response);
					}else{
						res.sendRedirect("/payrollsystem/login.jsp");
					}
				}else {
					res.sendRedirect("/payrollsystem/login.jsp");
				}
		} catch (Exception e) {
			logger.error(e.toString());
		}
	}

	public void init(FilterConfig filterConfig) throws ServletException {
		
	}

}
